package com.bjfu.intelligentrecognizeserver.service.impl;

import com.bjfu.intelligentrecognizeserver.mapper.CommonMapper;
import com.bjfu.intelligentrecognizeserver.mapper.SpeciesMapper;
import com.bjfu.intelligentrecognizeserver.pojo.entity.*;
import com.bjfu.intelligentrecognizeserver.pojo.vo.SpeciesVo;
import com.bjfu.intelligentrecognizeserver.service.SpeciesService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class SpeciesServiceImpl implements SpeciesService {
    private final SpeciesMapper speciesMapper;
    private final CommonMapper commonMapper;

    @Override
    public List<SpeciesInfo> getAllSpecies() {
        return speciesMapper.getAllSpecies();
    }

    @Override
    public Integer addSelectSpeciesInfo(SelectSpeciesInfo selectSpeciesInfo) {
        speciesMapper.insertSelectSpecies(selectSpeciesInfo);
        return selectSpeciesInfo.getId();
    }

    /*@Override
    public Integer postSelectSpeciesInfo(SelectSpeciesInfoDTO selectSpeciesInfoDTO) {
        Map<String, String> category = selectSpeciesInfoDTO.getCategory();
        String jie = category.get("jie");
        Integer jieId = commonMapper.selectJieIdByJie(jie);
        String men = category.get("men");
        Integer menId = commonMapper.selectMenIdByMen(men);
        String gang = category.get("gang");
        Integer gangId = commonMapper.selectGangIdByGang(gang);
        String mu = category.get("mu");
        Integer muId = commonMapper.selectMuIdByMu(mu);
        String ke = category.get("ke");
        Integer keId = commonMapper.selectKeIdByKe(ke);
        String shu = category.get("shu");
        Integer shuId = commonMapper.selectShuIdByShu(shu);
        String zhong = category.get("zhong");
        Integer zhongId = commonMapper.selectCategoryIdByCategory(zhong);

        //将DTO转换成entity
        SelectSpeciesInfo selectSpeciesInfo = SelectSpeciesInfo.builder()
                .chineseName(selectSpeciesInfoDTO.getChineseName())
                .latinName(selectSpeciesInfoDTO.getLatinName())
                .longitude(selectSpeciesInfoDTO.getLongitude())
                .latitude(selectSpeciesInfoDTO.getLatitude())
                .method(selectSpeciesInfoDTO.getMethod())
                .description(selectSpeciesInfoDTO.getDescription())
                .sex(selectSpeciesInfoDTO.getSex())
                .jieId(jieId)
                .menId(menId)
                .gangId(gangId)
                .muId(muId)
                .keId(keId)
                .shuId(shuId)
                .categoryId(zhongId)
                .build();

        speciesMapper.insertSelectSpecies(selectSpeciesInfo);
        return 0;
    }*/

    @Override
    public Integer getIdByChineseName(String chineseName) {

        return speciesMapper.getIdByChineseName(chineseName);
    }

    @Override
    public List<Kingdom> getKingdom() {
        return speciesMapper.getKingdom();
    }

    @Override
    public Integer getMenIdByName(String name) {

        return speciesMapper.getMenIdByName(name);
    }

    @Override
    public List<String> getMenListByKingdomId(Integer parentId) {
        return speciesMapper.getMenListByKingdomId(parentId);
    }

    @Override
    public List<String> getGangListByMenId(Integer parentId) {
        return speciesMapper.getGangListByMenId(parentId);
    }

    @Override
    public Integer getGangIdByName(String name) {
        return speciesMapper.getGangIdByName(name);
    }

    @Override
    public Integer getMuIdByName(String name) {
        return speciesMapper.getMuIdByName(name);
    }

    @Override
    public List<String> getMuListByGangId(Integer parentId) {
        return speciesMapper.getMuListByGangId(parentId);
    }

    @Override
    public Integer getKeIdByName(String name) {
        return speciesMapper.getKeIdByName(name);
    }

    @Override
    public List<String> getKeListByMuId(Integer parentId) {
        return speciesMapper.getKeListByMuId(parentId);
    }

    @Override
    public SpeciesVo getSpeciesInfoByName(String speciesName) {
        SpeciesInfo speciesInfo = speciesMapper.getSpeciesInfoBySpeciesName(speciesName);

        return SpeciesVo.builder()
                .latinName(speciesInfo.getLatinName())
                .chineseName(speciesInfo.getChineseName())
                .description(speciesInfo.getDescription())
                .id(speciesInfo.getId())
                .build();
    }

    @Override
    public List<Integer> addSelectPhotos(SelectPhotos selectPhotos) {
        List<Integer> selectPhotoIds = new ArrayList<>();
        for (String imagePath : selectPhotos.getSelectedPhotos()) {
            SelectImage selectImage = SelectImage.builder()
                    .selectSpeciesInfoId(selectPhotos.getSelectSpeciesInfoId())
                    .userId(selectPhotos.getUserId())
                    .imagePath(imagePath).build();
             speciesMapper.insertSelectPhoto(selectImage);
            selectPhotoIds.add(selectImage.getId());
        }
        return selectPhotoIds;
    }

    @Override
    public List<Integer> addSelectAudios(SelectAudios selectAudios) {
        List<Integer> selectAudioIds = new ArrayList<>();
        for (String audioPath : selectAudios.getSelectedAudios()) {
            SelectAudio selectAudio = SelectAudio.builder()
                    .selectSpeciesInfoId(selectAudios.getSelectSpeciesInfoId())
                    .userId(selectAudios.getUserId())
                    .AudioPath(audioPath).build();
             speciesMapper.insertSelectAudio(selectAudio);
             /*插入表格后此时selectAudio id 被mybatis直接赋值成功*/
            selectAudioIds.add(selectAudio.getId());
        }
        return selectAudioIds;
    }
}
